package com.enjoyor.soa.traffic.rest.tsms.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tsms.TaskExecuteRecordDto;
import com.enjoyor.soa.traffic.api.dto.tsms.TaskInfoDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.tsms.dubbo.ITaskExecuteRecordConsumeService;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/taskRecord" })
@Api("任务执行记录相关接口")
@CrossOrigin
public class TaskExecuteRecordController {

    @Autowired
    private ITaskExecuteRecordConsumeService taskExecuteRecordService;

    @GetMapping(value = { "/selectExecuteTaskRecordById" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("根据recordId查询任务执行记录信息")
    @ApiImplicitParam(name = "recordId", value = "执行记录编号", paramType = "query", required = true, dataType = "String")
    public ResultPojo getExecuteTaskRecordById(
    		@RequestParam(defaultValue = "", value="recordId", required=true) String recordId, 
    		HttpServletRequest request) {
        return taskExecuteRecordService.getExecuteTaskRecordById(recordId);
    }
    
	@GetMapping(value = { "/selectExecuteTaskRecordList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response=TaskInfoDto.class, code = 0, message = "")
    @ApiOperation("任务执行记录列表信息查询")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "recordId", value = "执行记录编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "executeTaskId", value = "执行任务编号(多个用,分隔)", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "taskId", value = "任务编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "recordTypeId", value = "记录类型编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "isWarn", value = "是否报警（是1，否0）", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "beginTime", value = "记录开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "endTime", value = "记录结束时间", paramType = "query", dataType = "String")})
	public ResultPojo getExecuteTaskRecordList(
			@RequestParam(defaultValue = "",value="recordId",required=false) String recordId,
			@RequestParam(defaultValue = "",value="executeTaskId",required=false) String executeTaskId,
			@RequestParam(defaultValue = "",value="taskId",required=false) String taskId,
			@RequestParam(defaultValue = "",value="recordTypeId",required=false) String recordTypeId,
			@RequestParam(defaultValue = "",value="isWarn",required=false) String isWarn,
			@RequestParam(defaultValue = "",value="beginTime",required=false) String beginTime,
			@RequestParam(defaultValue = "",value="endTime",required=false) String endTime,
			HttpServletRequest request) {
		return taskExecuteRecordService.getExecuteTaskRecordList(recordId, executeTaskId, taskId, recordTypeId, isWarn, 
				beginTime, endTime);
	}
	
    @SuppressWarnings({ "rawtypes" })
    @GetMapping(value = { "/selectExecuteTaskRecordPage" }, produces = { "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("任务执行记录分页信息查询")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "recordId", value = "执行记录编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "executeTaskId", value = "执行任务编号(多个用,分隔)", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "taskId", value = "任务编号", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "recordTypeId", value = "记录类型编号", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "isWarn", value = "是否报警（是1，否0）", paramType = "query", dataType = "String"),
    	@ApiImplicitParam(name = "beginTime", value = "记录开始时间", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "endTime", value = "记录结束时间", paramType = "query", dataType = "String")})
    public ResultPojo getExecuteTaskRecordPage(
			@RequestParam(defaultValue = "",value="recordId",required=false) String recordId,
			@RequestParam(defaultValue = "",value="executeTaskId",required=false) String executeTaskId,
			@RequestParam(defaultValue = "",value="taskId",required=false) String taskId,
			@RequestParam(defaultValue = "",value="recordTypeId",required=false) String recordTypeId,
			@RequestParam(defaultValue = "",value="isWarn",required=false) String isWarn,
			@RequestParam(defaultValue = "",value="beginTime",required=false) String beginTime,
			@RequestParam(defaultValue = "",value="endTime",required=false) String endTime,
    		Page page, HttpServletRequest request) {
        return taskExecuteRecordService.getExecuteTaskRecordPage(page, recordId, executeTaskId, taskId, recordTypeId, 
        		isWarn, beginTime, endTime);
    }

   
    @PostMapping(value = { "/addExecuteTaskRecord" }, consumes = { "application/json; charset=UTF-8" }, produces = {
            "application/json; charset=UTF-8" })
    @ResponseBody
    @ApiOperation("新增任务执行记录信息")
    public ResultPojo addExecuteTaskRecord(@RequestBody TaskExecuteRecordDto taskExecuteRecordDto, HttpServletRequest request) {
    	String personName = ((UserDto) request.getAttribute("currentUser")).getPersonName();
        return taskExecuteRecordService.insertRecord(taskExecuteRecordDto, personName);
    }
}
